package wfmanage.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.MyLog;

import edu.wfmanage.models.Claim;
import edu.wfmanage.models.User;
/**
Author: Satya Deepthi Bhagi
**/
public class financeResult extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private String financeResult;
	private int RequestId;
	private String financeComments;
	private String Submit;
	private String username;
	private List<Claim> financeitems;
	private int LTALimit;
	private int amountApproved;
	private List<User> userDetails;
	String toList="";
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		if (user == null) {
			user = new User();
		}
		username=user.getUsername();
		if (this.financeComments.isEmpty()) {
		    return "initial_entry";
        } 
		if (Submit.equalsIgnoreCase("Back")) {
			System.out.println("In back");
			financeitems=new ArrayList<Claim>();
			LTALimit=Claim.getLTALimit("select LTA_Limit from employee where user_id= '" + user.getUserid()+"'");
			financeitems = Claim.findfinanceitems("select * from request where VERIFY_DPT_STATUS ='VerificationPassed'and FINANCE_DPT_STATUS='finance approval pending'and status='pending'",LTALimit);
			return "back";
		}
		if (Submit.equalsIgnoreCase("Finish")){
			String updatesql=null;
			System.out.println("In updating db");
			if(financeResult.equals("Approved")){
		 updatesql="update request set FINANCE_DPT_STATUS='"+ getFinanceResult()+"', APPROVED_AMT="+ getAmountApproved() + ", FINANCE_DPT_ACTION_TIME=sysdate(),FINANCE_DPT_COMMENT='"+
				getFinanceComments()+"',FINANCE_APPROVAL_BY='"+user.getUsername() +"', status='Claim Approved' where req_id="+getRequestId();
		 Claim.updatedb(updatesql);
		 financeitems=new ArrayList<Claim>();
			LTALimit=Claim.getLTALimit("select LTA_Limit from employee where user_id= '" + user.getUserid()+"'");
			financeitems = Claim.findfinanceitems("select * from request where VERIFY_DPT_STATUS ='VerificationPassed'and FINANCE_DPT_STATUS='finance approval pending'and status='pending'",LTALimit);
			userDetails=User.userDetails("e,request r where r.req_id="+getRequestId()+" and e.user_id= r.user_id");
			email mail=new email();
			for(User u: userDetails){
				MyLog.log("user is"+u.getEmailid());
				System.out.println("user is"+u.getEmailid());
				mail.sendmail(u.getUsername(),u.getEmailid(),"user",RequestId,"NA");
				MyLog.log("An email has been sent to"+u.getEmailid());
				System.out.println("An email has been sent to"+u.getEmailid());
			}			
			return "approved";
			}
			if(financeResult.equals("Rejected")){
				updatesql="update request set FINANCE_DPT_STATUS='"+ getFinanceResult()+"', APPROVED_AMT="+ getAmountApproved() + ", FINANCE_DPT_ACTION_TIME=sysdate(),FINANCE_DPT_COMMENT='"+
						getFinanceComments()+"',FINANCE_APPROVAL_BY='"+user.getUsername() +"', status='Claim Rejected' where req_id="+getRequestId();
				financeitems=new ArrayList<Claim>();
				LTALimit=Claim.getLTALimit("select LTA_Limit from employee where user_id= '" + user.getUserid()+"'");
				financeitems = Claim.findfinanceitems("select * from request where VERIFY_DPT_STATUS ='VerificationPassed'and FINANCE_DPT_STATUS='finance approval pending'and status='pending'",LTALimit);
				Claim.updatedb(updatesql);
					}
					
		     }
		userDetails=User.userDetails("e,request r where r.req_id="+getRequestId()+" and e.user_id= r.user_id");
		email mail=new email();
		for(User u: userDetails){
			System.out.println("user is"+u.getEmailid());
	//toList+="\""+u.getEmailid()+"\", ";
			mail.sendmail(u.getUsername(),u.getEmailid(),"user",RequestId,"NA");
			System.out.println("An email has been sent to"+u.getEmailid());
		}	
		
		 		return "rejected";
	}

	
	
	public List<Claim> getFinanceitems() {
		return financeitems;
	}



	public void setFinanceitems(List<Claim> financeitems) {
		this.financeitems = financeitems;
	}



	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSubmit() {
		return Submit;
	}
	public void setSubmit(String submit) {
		Submit = submit;
	}

	public int getRequestId() {
		return RequestId;
	}

	public void setRequestId(int requestId) {
		RequestId = requestId;
	}


	public String getFinanceResult() {
		return financeResult;
	}


	public void setFinanceResult(String financeResult) {
		this.financeResult = financeResult;
	}


	public String getFinanceComments() {
		return financeComments;
	}


	public void setFinanceComments(String financeComments) {
		this.financeComments = financeComments;
	}


	public int getLTALimit() {
		return LTALimit;
	}



	public void setLTALimit(int lTALimit) {
		LTALimit = lTALimit;
	}



	public int getAmountApproved() {
		return amountApproved;
	}

	public void setAmountApproved(int amountApproved) {
		this.amountApproved = amountApproved;
	}

	
	
	
}
